package com.paiai.lslease.action.apphouse;

import com.paiai.lslease.auto.model.AppLock;
import com.paiai.lslease.base.BaseAction;
import com.paiai.lslease.model.AppLockInfo;
import com.paiai.lslease.service.apphouse.AppHouseService;
import com.paiai.lslease.service.apphouse.AppLockService;
import com.paiai.lslease.service.apphouse.AppSocketService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

public class AppLockAction extends BaseAction {

    private static final Logger logger = LoggerFactory.getLogger(AppLockAction.class);
    
    private AppLockService appLockService;
    private AppHouseService appHouseService;
    private AppSocketService appSocketService;

    private String houseId;
    private String operateUserId;
    private String name;
    private String password;
    private String macAddress;
    private String roomName;
    private String socketId;
    private String openRecordFlag;
    private String firewareVersion;
    private String maxUserNum;
    private String usedUserNum;
    private String battery;
    private String batteryState;
    private String lockedState;
    private String onlineState;
    private String item2;//nb设备编号
    private String item3;//nb设备imei号
    private String item4;//nb设备类型

    private String lockId;

    private String userId;

    public void setHouseId(String houseId) {
        this.houseId = houseId;
    }

    public void setOperateUserId(String operateUserId) {
        this.operateUserId = operateUserId;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setMacAddress(String macAddress) {
        this.macAddress = macAddress;
    }

    public void setRoomName(String roomName) {
        this.roomName = roomName;
    }

    public void setSocketId(String socketId) {
        this.socketId = socketId;
    }

    public void setOpenRecordFlag(String openRecordFlag) {
        this.openRecordFlag = openRecordFlag;
    }

    public void setFirewareVersion(String firewareVersion) {
        this.firewareVersion = firewareVersion;
    }

    public void setMaxUserNum(String maxUserNum) {
        this.maxUserNum = maxUserNum;
    }

    public void setUsedUserNum(String usedUserNum) {
        this.usedUserNum = usedUserNum;
    }

    public void setBattery(String battery) {
        this.battery = battery;
    }

    public void setBatteryState(String batteryState) {
        this.batteryState = batteryState;
    }

    public void setLockedState(String lockedState) {
        this.lockedState = lockedState;
    }

    public void setOnlineState(String onlineState) {
        this.onlineState = onlineState;
    }

    public void setAppLockService(AppLockService appLockService) {
        this.appLockService = appLockService;
    }

    public void setAppHouseService(AppHouseService appHouseService) {
        this.appHouseService = appHouseService;
    }

    public void setLockId(String lockId) {
        this.lockId = lockId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public void setAppSocketService(AppSocketService appSocketService) {
        this.appSocketService = appSocketService;
    }

    public String getItem2() {
        return item2;
    }

    public void setItem2(String item2) {
        this.item2 = item2;
    }

    public String getItem3() {
        return item3;
    }

    public void setItem3(String item3) {
        this.item3 = item3;
    }

    public String getItem4() {
        return item4;
    }

    public void setItem4(String item4) {
        this.item4 = item4;
    }

    @Override
    public String toString() {
        return "AppLockAction{" +
                "appLockService=" + appLockService +
                ", appHouseService=" + appHouseService +
                ", appSocketService=" + appSocketService +
                ", houseId='" + houseId + '\'' +
                ", operateUserId='" + operateUserId + '\'' +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", macAddress='" + macAddress + '\'' +
                ", roomName='" + roomName + '\'' +
                ", socketId='" + socketId + '\'' +
                ", openRecordFlag='" + openRecordFlag + '\'' +
                ", firewareVersion='" + firewareVersion + '\'' +
                ", maxUserNum='" + maxUserNum + '\'' +
                ", usedUserNum='" + usedUserNum + '\'' +
                ", battery='" + battery + '\'' +
                ", batteryState='" + batteryState + '\'' +
                ", lockedState='" + lockedState + '\'' +
                ", onlineState='" + onlineState + '\'' +
                ", item2='" + item2 + '\'' +
                ", item3='" + item3 + '\'' +
                ", item4='" + item4 + '\'' +
                ", lockId='" + lockId + '\'' +
                ", userId='" + userId + '\'' +
                '}';
    }

    /**
     * 新增一个设备
     */
    public void insertLock() {
        try {
            logger.info(this.toString());
            if (StringUtils.isEmpty(houseId)) {
                sendFailureMsg("房源ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(houseId)) {
                sendFailureMsg("房源ID格式错误");
                return;
            }
            if (!appHouseService.checkHouseExist(Integer.parseInt(houseId))) {
                sendFailureMsg("房源不存在");
            }
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("添加设备的用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("添加设备的用户ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("添加设备的用户不存在");
                return;
            }
//            if (!appSysUserService.userBelongInstallWorker(Integer.parseInt(operateUserId))) {
//                sendFailureMsg("用户不是安装工，不能添加设备");
//                return;
//            }
            if (StringUtils.isEmpty(name)) {
                name = operateUserId + "的新设备";
            }
//            if (StringUtils.isEmpty(macAddress)) {
//                sendFailureMsg("设备mac地址不能为空");
//                return;
//            }
//            if (appLockService.lockExistByHouseIdAndLockMac(Integer.parseInt(houseId), macAddress)) {
//                sendFailureMsg("房源下已存在该设备，请不要重复添加");
//                return;
//            }
            if (StringUtils.isEmpty(roomName)) {
                roomName = operateUserId + "的新房间";
            }
            if (!StringUtils.isEmpty(socketId)) {
                if (!StringUtils.isNumeric(socketId)) {
                    sendFailureMsg("插座ID格式错误");
                    return;
                }
                if (!appSocketService.checkSocketExist(Integer.parseInt(socketId))) {
                    sendFailureMsg("插座不存在");
                    return;
                }
            }
            if (StringUtils.isEmpty(openRecordFlag) || !(openRecordFlag.equalsIgnoreCase("1") || openRecordFlag.equalsIgnoreCase("2"))) {
                openRecordFlag = "1";//添加设备时，开门记录上传功能默认开启
            }
            if (StringUtils.isEmpty(maxUserNum) || !StringUtils.isNumeric(maxUserNum)) {
                maxUserNum = "10";//添加设备时，最大租客数量为10
            }
            usedUserNum = "0";
            if (StringUtils.isEmpty(battery)) {
                battery = "100";//添加设备时，电量默认100
            }
            if (StringUtils.isEmpty(batteryState) || !(batteryState.equalsIgnoreCase("1") || batteryState.equalsIgnoreCase("2"))) {
                batteryState = "1";//添加设备时，电量状态默认正常
            }
            if (StringUtils.isEmpty(lockedState) || !(lockedState.equalsIgnoreCase("1") || lockedState.equalsIgnoreCase("2"))) {
                lockedState = "1";//添加设备时，默认已上锁
            }
            if (StringUtils.isEmpty(onlineState) || !(onlineState.equalsIgnoreCase("1") || onlineState.equalsIgnoreCase("2"))) {
                onlineState = "1";//添加设备时，默认离线
            }
            if (StringUtils.isEmpty(firewareVersion)) {
                sendFailureMsg("设备固件版本号不能为空");
                return;
            }
            AppLock appLockToInsert = new AppLock();
            appLockToInsert.setHouseId(Integer.valueOf(houseId));
            appLockToInsert.setBelongUserId(Integer.valueOf(operateUserId));
            appLockToInsert.setName(name);
            appLockToInsert.setPassword(password);
            appLockToInsert.setMacAddress(macAddress);
            appLockToInsert.setRoomName(roomName);
            if (!StringUtils.isEmpty(socketId)) {
                appLockToInsert.setSocketId(Integer.parseInt(socketId));
            }
            appLockToInsert.setOpenRecordFlag(Integer.valueOf(openRecordFlag));
            appLockToInsert.setFirewareVersion(firewareVersion);
            appLockToInsert.setMaxUserNum(Integer.valueOf(maxUserNum));
            appLockToInsert.setUsedUserNum(Integer.valueOf(usedUserNum));
            appLockToInsert.setBattery(battery);
            appLockToInsert.setBatteryState(Integer.valueOf(batteryState));
            appLockToInsert.setLockedState(Integer.valueOf(lockedState));
            appLockToInsert.setOnlineState(Integer.valueOf(onlineState));
            appLockToInsert.setItem1("1");
            appLockToInsert.setItem2(item2);
            appLockToInsert.setItem3(item3);
            appLockToInsert.setItem4(item4);
            appLockToInsert.setInsertTime(new Date());
            appLockToInsert.setUpdateTime(new Date());
            AppLock appLock = appLockService.insertLock(appLockToInsert);
            if (appLock == null) {
                logger.info("appLock == null");
                sendFailureMsg("添加设备失败");
                return;
            }
            sendSuccesseMsg(appLock, "添加设备成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("添加设备失败");
        }
    }

    /**
     * 删除一个设备
     */
    public void deleteLockById() {
        try {
            logger.info(this.toString());
            if (StringUtils.isEmpty(lockId)) {
                sendFailureMsg("设备ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(lockId)) {
                sendFailureMsg("设备ID格式错误");
                return;
            }
            if (!appLockService.lockExistById(Integer.parseInt(lockId))) {
                sendFailureMsg("设备不存在");
                return;
            }
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作的用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作的用户ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作的用户不存在");
                return;
            }
            if (!appLockService.lockBelongUser(Integer.parseInt(lockId), Integer.parseInt(operateUserId))) {
                sendFailureMsg("设备不属于该该用户，不能删除");
                return;
            }
            boolean flag = appLockService.deleteLockById(Integer.parseInt(lockId));
            if (!flag) {
                logger.info("flag == false");
                sendFailureMsg("删除设备失败");
                return;
            }
            sendSuccesseMsg("删除设备成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("删除设备失败");
        }
    }

    /**
     * 修改一个设备
     */
    public void modifyLock() {
        try {
            logger.info(this.toString());
            if (StringUtils.isEmpty(lockId)) {
                sendFailureMsg("设备ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(lockId)) {
                sendFailureMsg("设备ID格式错误");
                return;
            }
            if (!appLockService.lockExistById(Integer.parseInt(lockId))) {
                sendFailureMsg("设备不存在");
                return;
            }
            AppLock appLockToUpdate = new AppLock();
            appLockToUpdate.setId(Integer.valueOf(lockId));
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作的用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作的用户ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作的用户不存在");
                return;
            }
            if (!appLockService.lockBelongUser(Integer.parseInt(lockId), Integer.parseInt(operateUserId))) {
                sendFailureMsg("设备不属于该该用户，不能编辑");
                return;
            }
            if (StringUtils.isEmpty(name) && StringUtils.isEmpty(password) && StringUtils.isEmpty(roomName)
                    && StringUtils.isEmpty(socketId) && StringUtils.isEmpty(openRecordFlag) && StringUtils.isEmpty(firewareVersion) && StringUtils.isEmpty(maxUserNum)
                    && StringUtils.isEmpty(usedUserNum) && StringUtils.isEmpty(battery) && StringUtils.isEmpty(batteryState) && StringUtils.isEmpty(lockedState) && StringUtils.isEmpty(onlineState)) {
                sendFailureMsg("请至少修改一个用户信息");
                return;
            }
            if (!StringUtils.isEmpty(socketId)) {
                if (!StringUtils.isNumeric(socketId)) {
                    sendFailureMsg("插座ID格式错误");
                    return;
                }
                if (!appSocketService.checkSocketExist(Integer.parseInt(socketId))) {
                    sendFailureMsg("插座不存在");
                    return;
                }
                appLockToUpdate.setSocketId(Integer.parseInt(socketId));
            }
            if (!StringUtils.isEmpty(openRecordFlag)) {
                if (!openRecordFlag.equalsIgnoreCase("2")) {
                    openRecordFlag = "1";
                }
                appLockToUpdate.setOpenRecordFlag(Integer.valueOf(openRecordFlag));
            }
            if (!StringUtils.isEmpty(maxUserNum) && !StringUtils.isNumeric(maxUserNum)) {
                sendFailureMsg("设备最大用户数格式错误");
                return;
            }
            if (!StringUtils.isEmpty(usedUserNum) && !StringUtils.isNumeric(usedUserNum)) {
                sendFailureMsg("设备已添加的用户数格式错误");
                return;
            }
            if (!StringUtils.isEmpty(battery) && !StringUtils.isNumeric(battery)) {
                sendFailureMsg("设备电量信息格式错误");
                return;
            }
            if (!StringUtils.isEmpty(batteryState) && !batteryState.equalsIgnoreCase("2")) {
                batteryState = "1";
            }
            if (!StringUtils.isEmpty(lockedState) && !lockedState.equalsIgnoreCase("2")) {
                lockedState = "1";
            }
            if (!StringUtils.isEmpty(onlineState) && !onlineState.equalsIgnoreCase("2")) {
                onlineState = "1";
            }
            appLockToUpdate.setName(name);
            appLockToUpdate.setRoomName(roomName);
            appLockToUpdate.setFirewareVersion(firewareVersion);
            appLockToUpdate.setPassword(password);
            if (!StringUtils.isEmpty(maxUserNum)) {
                appLockToUpdate.setMaxUserNum(Integer.valueOf(maxUserNum));
            }
            if (!StringUtils.isEmpty(usedUserNum)) {
                appLockToUpdate.setUsedUserNum(Integer.valueOf(usedUserNum));
            }
            appLockToUpdate.setBattery(battery);
            if (!StringUtils.isEmpty(batteryState)) {
                appLockToUpdate.setBatteryState(Integer.valueOf(batteryState));
            }
            if (!StringUtils.isEmpty(lockedState)) {
                appLockToUpdate.setLockedState(Integer.valueOf(lockedState));
            }
            if (!StringUtils.isEmpty(onlineState)) {
                appLockToUpdate.setOnlineState(Integer.valueOf(onlineState));
            }
            AppLock appLock = appLockService.modifyLock(appLockToUpdate);
            if (appLock == null) {
                logger.info("appLock == null");
                sendFailureMsg("更新设备失败");
                return;
            }
            sendSuccesseMsg(appLock, "更新设备成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("更新设备失败");
        }
    }

    /**
     * 查询房源下的所有设备
     */
    public void selectHouseAllLock() {
        try {
            logger.info(this.toString());
            if (StringUtils.isEmpty(houseId)) {
                sendFailureMsg("房源ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(houseId)) {
                sendFailureMsg("房源ID格式错误");
                return;
            }
            if (!appHouseService.checkHouseExist(Integer.parseInt(houseId))) {
                sendFailureMsg("房源不存在");
            }
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作的用户ID不能为空");
                return;
            }

            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作的用户ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作的用户不存在");
                return;
            }
            List<AppLockInfo> appLockInfoList = appLockService.selectHouseAllLock(Integer.parseInt(houseId));
            sendSuccesseMsg(appLockInfoList, "获取房源设备列表成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("获取房源设备列表失败");
        }
    }

    /**
     * 查询用户的所有设备
     */
    public void selectUserAllDevice() {
        try {
            logger.info(this.toString());
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作的用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作的用户ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作的用户不存在");
                return;
            }
            if (StringUtils.isEmpty(userId)) {
                sendFailureMsg("用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(userId)) {
                sendFailureMsg("用户ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("被查询的用户不存在");
                return;
            }
            List<AppLock> appLockList = appLockService.selectUserAllDevice(Integer.parseInt(userId));
            sendSuccesseMsg(appLockList, "查询用户的所有设备成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("查询用户的所有设备失败");
        }
    }

    /**
     * 根据设备ID查询单个设备信息
     */
    public void selectSingleDeviceById() {
        try {
            logger.info(this.toString());
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作的用户ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作的用户ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作的用户不存在");
                return;
            }
            if (StringUtils.isEmpty(lockId)) {
                sendFailureMsg("设备ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(lockId)) {
                sendFailureMsg("设备ID格式错误");
                return;
            }
            AppLock appLock = appLockService.selectLockById(Integer.parseInt(lockId));
            if (appLock == null) {
                sendFailureMsg("设备不存在");
                return;
            }
            sendSuccesseMsg(appLock, "根据设备ID查询单个设备信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("根据设备ID查询单个设备信息失败");
        }
    }
}
